//
//  ContentView.swift
//  MovieAppDemo
//
//  Created by Demo on 2025/1/25.
//

import SwiftUI

struct ContentView: View {
    @State private var selectedTab: String = "discover"
    @Namespace private var animationNamespace
    
    init() {
        // 强制应用进入暗黑模式
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
            windowScene.windows.forEach { window in
                window.overrideUserInterfaceStyle = .dark
            }
        }
        // 强制导航栏和表格视图为暗黑模式
        UINavigationBar.appearance().overrideUserInterfaceStyle = .dark
        UITableView.appearance().overrideUserInterfaceStyle = .dark
    }
    
    var body: some View {
        NavigationStack {
            ZStack(alignment: .bottom) {
                if selectedTab == "discover" {
                    DiscoverView()
                } else {
                    NowView()
                }
                
                ZStack {
                    Capsule()
                        .fill(.white)
                        .frame(width: 140, height: 50)
                        .offset(x: selectedTab == "discover" ? -80 : 80)
                        .animation(
                            .spring(response: 0.2, dampingFraction: 0.5, blendDuration: 0) // 弹跳动画
                                .delay(0.1), // 设置弹跳延迟
                            value: selectedTab
                        )
                    
                    HStack {
                        Button {
                            withAnimation(
                                .spring(response: 0.2, dampingFraction: 0.5, blendDuration: 0) // 弹跳动画
                                    .delay(0.1) // 弹跳延迟
                            ) {
                                selectedTab = "discover"
                            }
                            triggerHapticFeedback()  // 触发震动反馈
                        } label: {
                            Image(systemName: "star").foregroundColor(selectedTab == "discover" ? .gray : .white)
                            Text("发现").foregroundColor(selectedTab == "discover" ? .gray : .white)
                        }
                        .frame(width: 120, height: 20)
                        .clipShape(Capsule())
                        .padding(.horizontal, 10) // 设置水平间距
                        .padding(.vertical, 20)  // 设置垂直间距
                        
                        Button {
                            withAnimation(
                                .spring(response: 0.2, dampingFraction: 0.5, blendDuration: 0) // 弹跳动画
                                    .delay(0.1) // 弹跳延迟
                            ) {
                                selectedTab = "now"
                            }
                            triggerHapticFeedback()  // 触发震动反馈
                        } label: {
                            Image(systemName: "rotate.3d").foregroundColor(selectedTab == "discover" ? .white : .gray)
                            Text("现在就看").foregroundColor(selectedTab == "discover" ? .white : .gray)
                        }
                        .frame(width: 120, height: 20)
                        .clipShape(Capsule())
                        .padding(20)
                        
                    }
                    .foregroundStyle(.gray)
                    .font(.system(size: 20, weight: .semibold))
                }
                .background(.ultraThinMaterial)
                .clipShape(Capsule())
                .padding(.bottom)
                
            }
            .ignoresSafeArea(.container, edges: .bottom)
        }
    }
}

extension UINavigationController: @retroactive UIGestureRecognizerDelegate {
    override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }
}


#Preview {
    ContentView()
}
